package com.wuze.cloud.controller;

import com.wuze.cloud.model.SysRegisterData;
import com.wuze.cloud.model.SysUploadData;
import com.wuze.cloud.request.*;
import com.wuze.cloud.response.SysUserInfoPageResponse;
import com.wuze.cloud.response.SysUserPageResponse;
import com.wuze.cloud.response.SysUserResponse;
import com.wuze.cloud.result.Result;
import com.wuze.cloud.service.SysUserService;
import com.wuze.cloud.util.IpAddrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: wuze
 * @description: 用户模块控制器
 * @date: 2023/02/16
 */
@RestController
public class SysUserController {
    public static final String PREFIX_PATH = "/sys/user/";

    @Autowired
    private SysUserService userService;

    /**
     * 用户信息分页查询
     * @param query
     * @return SysUserPageResponse
     */
    @PostMapping(PREFIX_PATH + "findByPage/v1")
    public Result<SysUserPageResponse> findByPage(@RequestBody SysUserQuery query){
        return Result.success(userService.findByPage(query));
    }

    /**
     * 用户信息分页查询v2
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH + "findByPage/v2")
    public Result<SysUserInfoPageResponse> findByPageV2(@RequestBody SysUserQuery query){
        return Result.success(userService.findByPageV2(query));
    }


    /**
     * 用户登录接口
     *
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH + "login/v1")
    public Result<SysUserResponse> login(@Validated @RequestBody SysLoginUserQuery query,HttpServletRequest request) {
        System.out.println(IpAddrUtil.getIpAddr(request));
        return Result.success(userService.login(query));
    }

    /**
     * 用户注册接口
     *
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH + "register/v1")
    public Result<Boolean> register(@Validated @RequestBody SysCreateUserQuery query) {
        userService.register(query);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 注册获取邮箱验证码
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"getCode/v1")
    public Result<Boolean> getCodeByRegister(@Validated @RequestBody SysGetCodeQuery query) {
        userService.getCodeByRegister(query);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 找回密码获取邮箱验证码
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"getCodeResetPwd/v1")
    public Result<Boolean> getCodeByResetPwd(@Validated @RequestBody SysGetCodeQuery query) {
        userService.getCodeByResetPwd(query);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 管理员获取上传信息
     * @return
     */
    @PostMapping(PREFIX_PATH+"getUploadInfo/v1")
    public Result<List<SysUploadData>> getUploadInfo(){
        return Result.success(userService.getUploadInfo());
    }

    /**
     * 管理员获取最近注册信息
     * @param role
     * @return
     */
    @PostMapping(PREFIX_PATH+"getRegisterInfo/v1")
    public Result<List<SysRegisterData>> getRegisterInfo(@RequestParam("role") String role){
        return Result.success(userService.getRegisterInfo(role));
    }

    /**
     * 重置密码
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"resetPwd/v1")
    public Result<Boolean> resetPwd(@Validated @RequestBody SysResetPwdQuery query) {
        userService.resetPwd(query);
        return Result.success(Boolean.TRUE);
    }


    /**
     * 修改个人信息
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH + "update/v1")
    public Result<SysUserResponse> update(@Validated @RequestBody SysUpdateUserQuery query) {
        return Result.success(userService.update(query));
    }

    /**
     * 创建管理员
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"createAdmin/v1")
    public Result<Boolean> createAdmin(@Validated @RequestBody SysCreateUserQuery query) {
        userService.createAdmin(query);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 批量注销用户
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH+"batchDeleteUser/v1")
    public Result<Boolean> batchDeleteUser(@RequestBody SysDelUserQuery query) {
        userService.batchDeleteUser(query);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 修改用户个人信息
     * @param query
     * @return
     */
    @PostMapping(PREFIX_PATH + "updateUser/v1")
    public Result<Boolean> updateUser(@Validated @RequestBody SysAdminUpdateUserQuery query) {
        userService.adminUpdate(query);
        return Result.success(Boolean.TRUE);
    }

}
